##
# Class TimeCode: Drop Frame (DF) and Non Drop Frame (NDF) timecode manipulation
# 
# Author: Nicolas Pajoni 2010
#
# Changes: 
#	- __init__ start with tc="00:00:00:00" and use the method SetTC: Emiliano Billi
#	- Documentation: Emiliano Billi

# @package TimeCode
class TimeCode(object):

    def __init__(self, tc="00:00:00:00"):
	self.__hh = 0
	self.SetTC(tc)

    def __validateTC(self, hh, mm, ss, ff):
	if ff == 30:
	    #
	    # HACK: Hay segmentos en Ardome que estan erroneos
	    #	
	    ff = 29
	if ( (hh < 24 and hh >= 0) and (mm < 60 and mm >= 0) and (ss < 60 and ss >=0) and (ff < 30 and ff >= 0)):
	    return True
	else:
	    return False

    ## GetHour()
    #  @param self The object pointer.
    #  @retval Hour attribute
    def GetHour(self):
	return self.__hh

    ## GetMinutes()
    #  @param self The object pointer.
    #  @retval Minutes attribute
    def GetMinutes(self):
	return self.__mm


    ## GetSeconds()
    #  @param self The object pointer.
    #  @retval Seconds attribute
    def GetSeconds(self):
	return self.__ss

    ## GetFrames()
    #  @param self The object pointer.
    #  @retval Frames attribute    
    def GetFrames(self):
	return self.__ff


    ## GetDropFrame()
    #  @param self The object pointer.
    #  @retval True or False
    def GetDropFrame(self):
	return self.__df


    ## SetTC()
    #  @param self The object pointer.
    #  @param tc: String of the timecode -> "hh:mm:ss:ff" (NDF) or "hh:mm:ss;ff" (DF)
    #  @retval True or False
    def SetTC(self, tc):
	ndfList = tc.split(":")
	
	if len(ndfList) == 4:
	    self.__df = False
	    frames = int(ndfList[3])
	    seconds = int(ndfList[2])
	elif len(ndfList) == 3:
	    self.__df = True
	    dfList = ndfList[2].split(";")
	    
	    if len(dfList) == 2:
		frames = int(dfList[1])
		seconds = int(dfList[0])
	    else:
		return False
	
	else:
	    return False
	
	hour, minutes = int(ndfList[0]), int(ndfList[1])
	
	if self.__validateTC(hour, minutes, seconds, frames):
	    self.__hh = hour
	    self.__mm = minutes
	    self.__ss = seconds
	    self.__ff = frames
	else:
	    return False
	
	return True


    ## GetFramesFromZero()
    #  @param self The object pointer.
    #  @retval Return a total frames from "00:00:00:00"
    def GetFramesFromZero(self):
	if self.__df:
	    totalMinutes = 60 * self.__hh + self.__mm
	    framesNumber  = 108000 * self.__hh + 1800 * self.__mm + 30 * self.__ss + self.__ff - 2 * (totalMinutes - int(totalMinutes / 10))
	
	else:
	    frameRate = 30
	    totalSeconds = (self.__hh * 3600) + (self.__mm * 60) + self.__ss
	    framesNumber = (totalSeconds * frameRate) + self.__ff
	return framesNumber

